home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / png / zlib09 / minigzip.c < prev    next >
C/C++ Source or Header  |  1995-04-29  |  5KB  |  226 lines

  1. /* minigzip.c -- simulate gzip using the zlib compression library
  2.  * Copyright (C) 1995 Jean-loup Gailly.
  3.  * For conditions of distribution and use, see copyright notice in zlib.h 
  4.  */
  5.  
  6. /*
  7.  * minigzip is a minimal implementation of the gzip utility. This is
  8.  * only an example of using zlib and isn't meant to replace the
  9.  * full-featured gzip. No attempt is made to deal with file systems
  10.  * limiting names to 14 or 8+3 characters, etc... Error checking is
  11.  * very limited. So use minigzip only for testing; use gzip for the
  12.  * real thing. On MSDOS, use only on file names without extension
  13.  * or in pipe mode.
  14.  */
  15.  
  16. /* $Id: minigzip.c,v 1.3 1995/04/29 14:27:21 jloup Exp $ */
  17.  
  18. #include <stdio.h>
  19. #include "zlib.h"
  20.  
  21. extern void exit  __P((int));
  22. extern int unlink __P((const char *));
  23.  
  24. #ifdef STDC
  25. #  include <string.h>
  26. #endif
  27.  
  28. #ifdef MSDOS
  29. #  include <fcntl.h>
  30. #  include <io.h>
  31. #  define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
  32. #else
  33. #  define SET_BINARY_MODE(file)
  34. #endif
  35.  
  36. #define BUFLEN 4096
  37. #define MAX_NAME_LEN 1024
  38.  
  39. #define local static
  40. /* For MSDOS and other systems with limitation on stack size. For Unix,
  41.     #define local
  42.    works also.
  43.  */
  44.  
  45. char *prog;
  46.  
  47. void error           __P((char *msg));
  48. void gz_compress     __P((FILE   *in, gzFile out));
  49. void gz_uncompress   __P((gzFile in, FILE   *out));
  50. void file_compress   __P((char  *file));
  51. void file_uncompress __P((char  *file));
  52. void main            __P((int argc, char *argv[]));
  53.  
  54. /* ===========================================================================
  55.  * Display error message and exit
  56.  */
  57. void error(msg)
  58.     char *msg;
  59. {
  60.     fprintf(stderr, "%s: %s\n", prog, msg);
  61.     exit(1);
  62. }
  63.  
  64. /* ===========================================================================
  65.  * Compress input to output then close both files.
  66.  */
  67. void gz_compress(in, out)
  68.     FILE   *in;
  69.     gzFile out;
  70. {
  71.     local char buf[BUFLEN];
  72.     int len;
  73.     int err;
  74.  
  75.     for (;;) {
  76.     len = fread(buf, 1, sizeof(buf), in);
  77.     if (ferror(in)) {
  78.         perror("fread");
  79.         exit(1);
  80.     }
  81.     if (len == 0) break;
  82.  
  83.     if (gzwrite(out, buf, len) != len) error(gzerror(out, &err));
  84.     }
  85.     fclose(in);
  86.     if (gzclose(out) != Z_OK) error("failed gzclose");
  87. }
  88.  
  89. /* ===========================================================================
  90.  * Uncompress input to output then close both files.
  91.  */
  92. void gz_uncompress(in, out)
  93.     gzFile in;
  94.     FILE   *out;
  95. {
  96.     local char buf[BUFLEN];
  97.     int len;
  98.     int err;
  99.  
  100.     for (;;) {
  101.     len = gzread(in, buf, sizeof(buf));
  102.     if (len < 0) error (gzerror(in, &err));
  103.     if (len == 0) break;
  104.  
  105.     if (fwrite(buf, 1, len, out) != (uInt)len) error("failed fwrite");
  106.     }
  107.     if (fclose(out)) error("failed fclose");
  108.  
  109.     if (gzclose(in) != Z_OK) error("failed gzclose");
  110. }
  111.  
  112.  
  113. /* ===========================================================================
  114.  * Compress the given file: create a corresponding .gz file and remove the
  115.  * original.
  116.  */
  117. void file_compress(file)
  118.     char  *file;
  119. {
  120.     local char outfile[MAX_NAME_LEN];
  121.     FILE  *in;
  122.     gzFile out;
  123.  
  124.     strcpy(outfile, file);
  125.     strcat(outfile, ".gz");
  126.  
  127.     in = fopen(file, "rb");
  128.     if (in == NULL) {
  129.     perror(file);
  130.     exit(1);
  131.     }
  132.     out = gzopen(outfile, "wb");
  133.     if (out == NULL) {
  134.     fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile);
  135.     exit(1);
  136.     }
  137.     gz_compress(in, out);
  138.  
  139.     unlink(file);
  140. }
  141.  
  142.  
  143. /* ===========================================================================
  144.  * Uncompress the given file and remove the original.
  145.  */
  146. void file_uncompress(file)
  147.     char  *file;
  148. {
  149.     local char buf[MAX_NAME_LEN];
  150.     char *infile, *outfile;
  151.     FILE  *out;
  152.     gzFile in;
  153.     int len = strlen(file);
  154.  
  155.     strcpy(buf, file);
  156.  
  157.     if (len > 3 && strcmp(file+len-3, ".gz") == 0) {
  158.     infile = file;
  159.     outfile = buf;
  160.     outfile[len-3] = '\0';
  161.     } else {
  162.     outfile = file;
  163.     infile = buf;
  164.     strcat(infile, ".gz");
  165.     }
  166.     in = gzopen(infile, "rb");
  167.     if (in == NULL) {
  168.     fprintf(stderr, "%s: can't gzopen %s\n", prog, infile);
  169.     exit(1);
  170.     }
  171.     out = fopen(outfile, "wb");
  172.     if (out == NULL) {
  173.     perror(file);
  174.     exit(1);
  175.     }
  176.  
  177.     gz_uncompress(in, out);
  178.  
  179.     unlink(infile);
  180. }
  181.  
  182.  
  183. /* ===========================================================================
  184.  * Usage:  minigzip [-d] [files...]
  185.  */
  186.  
  187. void main(argc, argv)
  188.     int argc;
  189.     char *argv[];
  190. {
  191.     int uncompr = 0;
  192.     gzFile file;
  193.  
  194.     prog = argv[0];
  195.     argc--, argv++;
  196.  
  197.     if (argc > 0) {
  198.     uncompr = (strcmp(*argv, "-d") == 0);
  199.     if (uncompr) {
  200.         argc--, argv++;
  201.     }
  202.     }
  203.     if (argc == 0) {
  204.         SET_BINARY_MODE(stdin);
  205.         SET_BINARY_MODE(stdout);
  206.     if (uncompr) {
  207.         file = gzdopen(fileno(stdin), "rb");
  208.             if (file == NULL) error("can't gzdopen stdin");
  209.         gz_uncompress(file, stdout);
  210.     } else {
  211.         file = gzdopen(fileno(stdout), "wb");
  212.             if (file == NULL) error("can't gzdopen stdout");
  213.         gz_compress(stdin, file);
  214.     }
  215.     } else {
  216.     do {
  217.         if (uncompr) {
  218.         file_uncompress(*argv);
  219.         } else {
  220.         file_compress(*argv);
  221.         }
  222.     } while (argv++, --argc);
  223.     }
  224.     exit(0);
  225. }
  226.